﻿using ChinaCircleJerk.Data.Model;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChinaCircleJerk.Data
{
    public class Seeder
    {
        public static void Seed(ChinaCircleJerkDbContext db)
        {
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(db);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            RoleStore<Role> roleStore = new RoleStore<Role>(db);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);

            ApplicationUser userOne = new ApplicationUser();
            userOne = userManager.FindByName("movingon11");
            if (userOne == null)
            {
                userManager.Create(new ApplicationUser 
                { 
                    Email = "abethel1986@outlook.com",
                    Name = "Andrew Bethel",
                    UserName = "movingon11"
                }, password: "bE1e2ke3");
            }
            if (!roleManager.Roles.Any())
            {
                roleManager.Create(new Role { Name = "Admin" });
                roleManager.Create(new Role { Name = "Moderator" });
                roleManager.Create(new Role { Name = "User" });
            }
            if (!userManager.IsInRole(userOne.Id, "Admin"))
            {
                userManager.AddToRole(userOne.Id, "Admin");
            }
            if (!db.Posts.Any())
            {
                db.Posts.Add(new Post { UserId = userOne.Id, PostId = 1, Title = "Jerk jerk jerk all day long", Body = "Jerkin it up while I sing this song, don't y'all wish that you could code just half as good as me, faggots"});
                db.SaveChanges();
            }
        }
    }
}
